import 'package:get/get.dart';
import 'package:school_assistant_m/api/homework/homework_api.dart';
import 'package:school_assistant_m/app/routes/app_pages.dart';
import 'package:school_assistant_m/store/login_store.dart';

class HomeworkController extends GetxController with StateMixin {
  //TODO: Implement HomeworkController

  LoginStore store = LoginStore();
  HomeworkApi courseApi = Get.put(HomeworkApi());
  bool isAdmin = false;
  String role = '';
  var id = ''.obs;
  List dataForm = [].obs;
  Map searchForm = {}.obs;
  Map result = {}.obs;

  void getUserInfo() async {
    isAdmin = await LoginStore().getAdmin();
    role = await LoginStore().getRole();
  }

  Future<void> getHomeworkList() async {
    Map res = await courseApi.getHomeworkList();
    print(res['results']['code']);
    if(res['results']['code']==200){
      dataForm = res['results']['data'];
      print('----------dataForm----------------');
      print(dataForm);
      change(dataForm,status: RxStatus.success());
    }else if(res['results']['code'] == 500){
      change(null, status: RxStatus.error('超时'));
    } else {
      change(null, status: RxStatus.error('网络错误'));
    }
  }

  Future<void> getHomeworkById(id) async {
    Map res = await courseApi.getHomeworkById(id);
    if (res['code']==200){
      Get.snackbar('${res['data']['homework_name']}'
          , '这是个${res['data']['courseType']}作业');
      // Get.toNamed(Routes.COURSE_DETAIL);
    }
  }

  @override
  void onInit() {
    getUserInfo();
    getHomeworkList();
    super.onInit();
  }

}
